package com.ascent.servlet;


import com.alibaba.fastjson.JSON;
import com.ascent.classes.Orderitem;
import com.ascent.classes.Orders;
import com.ascent.classes.Product;
import com.ascent.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 订单管理控制器
 */
@WebServlet("/orderServlet/*")
public class OrderServlet extends BaseServlet {
    OrderService orderService = new OrderService();

    /**
     * 查询所有订单返回前端显示
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void selectAllOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用dao方法获得参数
        List<Orders> orders = orderService.selectAllOrder();
        // 设置参数为json
        String order = JSON.toJSONString(orders);
        // 设置字符返回给前端json参数
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(order);
    }

    /**
     * 根据用户查询到用户信息返回给前端
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void lookUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 请求转移UserManagerServlet的通过用户id查找用户信息方法
        request.getRequestDispatcher("/user/selectByUid").forward(request,response);
    }

    /**
     * 根据订单id查询该订单的所有订单项并返回给前端对应订单项的商品信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findOrderitem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得请求值参数
        String id = request.getParameter("ordersid");
        int ordersid = Integer.parseInt(id);
        //调用dao方法获得要返回商品id集合
        List<Product> products= orderService.selectOrderitemByOrdersid(ordersid);
        // 设置参数为json
        String orderitems = JSON.toJSONString(products);
        // 设置字符返回给前端json参数
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(orderitems);
    }

    /**
     * 根据订单id和前端商品id查询订单项并删除
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void delOrderitem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得请求值参数
        String pid = request.getParameter("productid");
        String oid = request.getParameter("ordersid");
        int productid = Integer.parseInt(pid);
        int ordersid = Integer.parseInt(oid);
        // 调用dao方法
        Orderitem orderitem = orderService.selectOrderitemByproductidAndordersid(productid, ordersid);
        if (orderitem != null) {
            orderService.deleteOrderitemByproductidAndordersid(productid, ordersid);
            // 删除成功返回信号
            response.getWriter().write("success");
        } else {
            response.getWriter().write("error");
        }

    }

    /**
     * 根据订单id删除订单并删除订单内的订单项
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void delOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得请求值参数
        String idd = request.getParameter("id");
        int id = Integer.parseInt(idd);
        //调用dao方法
        Orders orders = orderService.selectOrderByid(id);
        if (orders != null) {
            orderService.deleteOrderByid(id);
            orderService.deleteOrderitemByordersid(id);
            // 删除成功返回信号
            response.getWriter().write("success");
        } else {
            response.getWriter().write("error");
        }
    }

    /**
     * 根据用户id查询订单返回给前端
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void UserfindOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得请求值参数
        String idd = request.getParameter("id");
        int usrid = Integer.parseInt(idd);
        //调用dao方法
        List<Orders> orders = orderService.selectOrderByusrid(usrid);
        // 设置参数为json
        String order = JSON.toJSONString(orders);
        // 设置字符返回给前端json参数
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(order);
    }
}